<?php

namespace StudyBuddy;

use StudyBuddy\FS\Path;

class AvatarParser extends StudyBuddyObject {

    public function __construct(Registry $o) {
        $this->oRegistry = $o;
    }

    /**
     * Parse the avatar file in $tempPath,
     * by creating small square image from it,
     * save into file system and then add path to new avatar
     * in User object as 'avatar' element
     *
     *
     * @param User $oUser
     * @param unknown_type $tempPath
     * @throws \StudyBuddy\Exception
     *
     * @return object $this
     */
    public function addAvatar(User $oUser, $tempPath) {

        d('$tempPath: ' . $tempPath);
        if (empty($tempPath)) {
            d('no avatar to add');
            return $this;
        }

        $size = $this->oRegistry->Ini->AVATAR_SQUARE_SIZE;

        $avatarDir = STUDYBUDDY_DATA_DIR . 'img' . DS . 'avatar' . DS . 'sqr' . DS;
        d('$avatarDir: ' . $avatarDir);

        $savePath = Path::prepare($oUser->getUid(), $avatarDir);
        d('$savePath: ' . $savePath);

        /**
         * Create avatar and save it
         * with compression level of 80% (small compression)
         */
        //try{
        $ImgParser = \StudyBuddy\Image\Editor::factory($this->oRegistry)
                ->loadImage($tempPath)
                ->makeSquare($size);
        $savePath .= $ImgParser->getExtension();
        $ImgParser->save($avatarDir . $savePath, null, 80);
        d('avatar saved to ' . $savePath);
        //} catch(\StudyBuddy\ImageException $e){
        //e('ImageException caught in: '.$e->getFile().' on line: '.$e->getLine().' error: '.$e->getMessage());
        //throw new \StudyBuddy\Exception('Unable to process your avatar image at this time');
        //}

        /**
         * Now remove tempPath file
         */
        @\unlink($tempPath);

        /**
         * Now add the path to avatar
         * to user object
         * save() is not invoked on User object here!
         * Either rely on auto-save (may not work in case User is
         * actually the Viewer object) or call save()
         * from a function that invoked this method
         */
        $oUser['avatar'] = $savePath;

        return $this;
    }

    /**
     * Given an path to avatar remove it from User object
     * if it looks like local avatar also remove it from
     * the file system
     *
     * @todo unfinished
     * 
     * @param object $oUser object of type User
     * @param string $src path to avatar
     *
     * @return object $this
     */
    public function removeAvatar(User $oUser, $src) {

        if (0 === \strncmp($src, 'http', 4)) {
            /**
             * External avatar, just remove from User object and done
             */
            $external = $oUser['avatar_external'];
            if ($src == $external) {
                $oUser['avatar_external'] = null;
            }

            /**
             * What to do if user wants to remove gravatar image?
             * We can mark nogravatar = true in user object
             * and then it will not use gravatar ever again
             * there will not be any way to undo it...
             */
            $oUser['noavatar'] = true;

            return $this;
        }

        /**
         * If this was not external avatar then must
         * remove from File System and 
         * then remove from User object
         */
        return $this;
    }

}
